<template>
  <div id="manageQuestionView">
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      page-position="bottom"
      @page-change="handlePageChange"
      @page-size-change="handlePageSizeChange"
    >
      <!---------------题目内容自定义渲染------------------------------>
      <template #content="{ record }">
        <label @click="currentModal = record.id + 'content'" class="clickable">
          {{ record.content }}
        </label>
        <a-modal
          title="题目内容"
          :visible="currentModal === record.id + 'content'"
          @ok="currentModal = ''"
          @cancel="currentModal = ''"
          status="info"
          hide-cancel
          :mask-closable="true"
          :mask-style="{
            background: 'rgba(126,126,126,0.59)',
          }"
        >
          <MdViewer :value="record.content" />
        </a-modal>
      </template>
      <!---------------标签列自定义渲染------------------------------>
      <template #tags="{ record }">
        <a-space>
          <a-tag
            v-for="(tag, index) in JSON.parse(record.tags)"
            :key="index"
            color="blue"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <!---------------操作列自定义渲染------------------------------>
      <template #option="{ record }">
        <a-space>
          <a-button type="primary" @click="doUpdate(record)">修改</a-button>
          <a-popconfirm
            content="确定要删除吗?"
            position="tr"
            type="warning"
            @ok="doDelete(record)"
          >
            <a-button type="primary" status="danger">删除</a-button>
          </a-popconfirm>
        </a-space>
      </template>
      <!---------------答案列自定义渲染------------------------------>
      <template #answer="{ record }">
        <label @click="currentModal = record.id + 'answer'" class="clickable">
          {{ record.answer }}
        </label>
        <a-modal
          title="题目内容"
          :visible="currentModal === record.id + 'answer'"
          @ok="currentModal = ''"
          @cancel="currentModal = ''"
          status="info"
          hide-cancel
          :mask-closable="true"
          :mask-style="{
            background: 'rgba(126,126,126,0.59)',
          }"
        >
          <MdViewer :value="record.answer" />
        </a-modal>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import { Modal } from "@arco-design/web-vue";
import { Question, QuestionControllerService } from "../../../api";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import MdViewer from "@/components/MdViewer.vue";

const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  current: 1,
  pageSize: 10,
});

const $modal = Modal;
const columns = [
  {
    title: "ID",
    dataIndex: "id",
    ellipsis: true,
    tooltip: { position: "bottom" },
  },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "内容",
    dataIndex: "content",
    ellipsis: true,
    slotName: "content",
  },
  {
    title: "标签",
    dataIndex: "tags",
    slotName: "tags",
  },
  {
    title: "答案",
    dataIndex: "answer",
    ellipsis: true,
    slotName: "answer",
    tooltip: { position: "bottom" },
  },
  {
    title: "提交数",
    dataIndex: "submitNum",
  },
  {
    title: "通过数",
    dataIndex: "acceptedNum",
  },
  {
    title: "判题配置",
    dataIndex: "judgeConfig",
    slotName: "judgeConfig",
    ellipsis: true,
    tooltip: { position: "bottom" },
  },
  {
    title: "判题用例",
    dataIndex: "judgeCase",
    ellipsis: true,
    tooltip: { position: "bottom" },
  },
  {
    title: "创建用户id",
    dataIndex: "userId",
    ellipsis: true,
    tooltip: { position: "bottom" },
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
  },
  {
    title: "操作",
    slotName: "option",
  },
];
const router = useRouter();
const currentModal = ref("");
/**
 * 加载数据
 */
const loadData = async () => {
  const rs = await QuestionControllerService.listQuestionByPageUsingPost(
    searchParams.value
  );
  if (rs.code === 0) {
    dataList.value = rs.data.records;
    total.value = rs.data.total;
  } else {
    message.error("获取加载失败" + rs.message);
  }
};

/**
 * 修改
 * @param question
 */
const doUpdate = (question: Question) => {
  router.push({
    path: "/update/question",
    query: {
      id: question.id,
    },
  });
};

/**
 * 处理页面更换
 * @param current
 */
const handlePageChange = (current: number) => {
  searchParams.value.current = current;
};
/**
 * 修改页内数量大小
 * @param size
 */
const handlePageSizeChange = (size: number) => {
  searchParams.value.pageSize = size;
};
/**
 * 删除
 * @param question
 */
const doDelete = async (question: Question) => {
  const rs = await QuestionControllerService.deleteQuestionUsingPost({
    id: question.id,
  });
  if (rs.code === 0) {
    message.success("删除成功");
    loadData();
  } else {
    message.error("删除失败,原因:" + rs.message);
  }
};

/**
 * 页面加载时触发数据加载
 */
onMounted(() => {
  console.log();
});

/**
 * 监听searchParam的变化,改变时触发页面的数据加载
 */
watchEffect(() => {
  let { current } = searchParams.value; //显式调用变量,使监控能够识别到
  loadData();
});
</script>

<style scoped>
.clickable {
  cursor: pointer;
  padding: 10px;
  border-radius: 10px;
}

.clickable:hover {
  background: rgba(57, 213, 213, 0.45);
}
</style>
